﻿/*******************************************************************************
 * Copyright © 2017 FriendStar.Framework 版权所有
 * Author: FZH
 * Description: FriendStar快速开发平台
 * Website：http://www.FriendStar.cn
*********************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using SqlSugarRepository;
using FriendStar.Code;
using FriendStar.Domain.Entity.BOM;
using FriendStar.Domain.IRepository.BOM;
using FriendStar.Repository.BOM;

namespace FriendStar.Application.BOM
{
    /// <summary>
	/// 数据表 CLDE_11_MX_CLDEerialQuota 对应的数据应用功能类。
	/// </summary>
	public class CLDE_11_MX_CLDEerialQuotaApp
    {       
        private string UseUnit = Configs.GetValue("UseUnit");  

        private ICLDE_11_MX_CLDEerialQuotaRepository service = new CLDE_11_MX_CLDEerialQuotaRepository();

        public List<CLDE_11_MX_CLDEerialQuotaEntity> GetList(Pagination pagination, string keyword)
        {
            var expression = ExtLinq.True<CLDE_11_MX_CLDEerialQuotaEntity>();
            expression = expression.And(t => t.UseUnit == UseUnit);
            if (!string.IsNullOrEmpty(keyword))
            {
                expression = expression.And(t => t.CldeID == keyword);
            }
            return service.FindList(expression, pagination);
        }

        public CLDE_11_MX_CLDEerialQuotaEntity GetForm(string keyValue)
        {
            return service.FindEntity(keyValue);
        }

        public CLDE_11_MX_CLDEerialQuotaEntity GetItem(string id)
        {
            var expression = ExtLinq.True<CLDE_11_MX_CLDEerialQuotaEntity>();
            expression = expression.And(t => t.UseUnit == UseUnit);
            if (!string.IsNullOrEmpty(id))
            {
                expression = expression.And(t => t.CldeID == id);
            }
            return service.FindEntity(expression);
        }

        public void DeleteForm(string keyValue)
        {
            service.Delete(t => t.CldeID == keyValue);
        }

        public void SubmitForm(CLDE_11_MX_CLDEerialQuotaEntity entity, string keyValue)
        {
            if (!string.IsNullOrEmpty(keyValue))
            {
                entity.FUpdateBy = OperatorProvider.Provider.GetCurrent().UserName;
                entity.FUpdateDate = DateTime.Now.ToString("yyyy-MM-dd");
                entity.FUpdateTime = DateTime.Now.ToString("HH:mm:ss");
                service.Update(entity);
            }
            else
            {
                entity.CldeID = Common.GuId();
                entity.UseUnit = UseUnit;
                entity.FCreateBy = OperatorProvider.Provider.GetCurrent().UserName;
                entity.FCreateDate = DateTime.Now.ToString("yyyy-MM-dd");
                entity.FCreateTime = DateTime.Now.ToString("HH:mm:ss");
                entity.FUpdateBy = OperatorProvider.Provider.GetCurrent().UserName;
                entity.FUpdateDate = DateTime.Now.ToString("yyyy-MM-dd");
                entity.FUpdateTime = DateTime.Now.ToString("HH:mm:ss");
                service.Insert(entity);
            }
        }

        public void UpdateForm(CLDE_11_MX_CLDEerialQuotaEntity entity)
        {
            service.Update(entity);
        }

        public bool CheckedEntity(CLDE_11_MX_CLDEerialQuotaEntity entity)
        {
            var expression = ExtLinq.True<CLDE_11_MX_CLDEerialQuotaEntity>();
            expression = expression.And(t => t.UseUnit == UseUnit);
            expression = expression.And(t => t.FProductionNO == entity.FProductionNO);
            if (!string.IsNullOrEmpty(entity.CldeID))
            {
                expression = expression.And(t => t.CldeID != entity.CldeID);
            }
            var oldEntityList = service.FindList(expression);
            return oldEntityList.Count == 0;
        }

        public int PublishEntitys(string ids)
        {
            string sqlString = "UPDATE [CLDE_11_MX_CLDEerialQuota] SET [MaintainStatus]='1',FUpdateBy='" + OperatorProvider.Provider.GetCurrent().UserName + "',FUpdateDate='" + DateTime.Now.ToString("yyyy-MM-dd") + "',FUpdateTime='" + DateTime.Now.ToString("HH:mm:ss") + "' WHERE [MaintainStatus]='0' AND [CldeID] IN (" + ids + ")";
            return service.ExecSql(sqlString);
        }
    }
}